{extend name='comm/base' /}

{block name='header'}
{include file='comm/header'}
{/block}
{block name='slider'}
{include file='comm/slider'}
{/block}

{block name='main'}
<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="{:url('admin.index')}">首页</a>
                </li>
                <li class="active">关系管理</li>
            </ul><!-- /.breadcrumb -->
        </div>

        <div class="page-content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="row">
                        <div class="col-xs-12">
                            <!--导入导出-->
                            <div class="clearfix btn-position" >
                                    <button class="btn btn-primary" type="button" id="add_btn" data-toggle="modal"
                                            data-target="#addModal">
                                        新增
                                    </button>
                            </div>
                            <div class="space"></div>
                            <div class="table-header">
                                关系管理
                            </div>

                            <!-- div.table-responsive -->

                            <!-- div.dataTables_borderWrap -->
                            <div>

                                <table id="dynamic-table" class="table table-striped table-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>关系名称</th>
                                        <th>创建时间</th>
                                        <th>
                                         更新时间
                                        </th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {volist name='list' id='relation'}
                                    <tr>
                                        <td class="center">
                                            {$relation.id}
                                        </td>
                                        <td>
                                              {$relation.name}
                                        </td>
                                        <td>{$relation.create_time|date='Y-m-d H:i:s'}</td>
                                        <td>{$relation.update_time|date='Y-m-d H:i:s'}</td>
                                        <td>
                                            <div class="hidden-sm hidden-xs action-buttons">
                                                <a class="green" href="javascript:void(0);" data-id="{$relation.id}" data-toggle="modal"
                                                   data-target="#updateModal">
                                                    <i class="ace-icon fa fa-pencil bigger-130"></i>
                                                </a>
                                                <a class="red" href="javascript:void(0);" onclick ="delete_relation({$relation.id})"   >
                                                    <i class="ace-icon fa fa-trash-o bigger-130"></i>
                                                </a>
                                            </div>

                                            <div class="hidden-md hidden-lg">
                                                <div class="inline pos-rel">
                                                    <button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                                        <i class="ace-icon fa fa-caret-down icon-only bigger-120"></i>
                                                    </button>

                                                    <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                                        <li>
                                                            <a href="javascript:void(0);" class="tooltip-success" data-rel="tooltip" title="Edit">
                                                                <span class="green" data-id="{$relation.id}" data-toggle="modal"
                                                                      data-target="#updateModal">
                                                                    <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                                                                </span>
                                                            </a>
                                                        </li>

                                                        <li>
                                                            <a href="javascript:void(0);" onclick ="delete_relation({$relation.id})" class="tooltip-error" data-rel="tooltip" title="Delete">
                                                                <span class="red">
                                                                    <i class="ace-icon fa fa-trash-o bigger-120"></i>
                                                                </span>
                                                            </a>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    {/volist}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- PAGE CONTENT ENDS -->
                    <div class="data-paginate">
                        <div class="total"> 共{$list->total()}条记录;</div>
                        <div class="p_paginate">{$list->render()|raw}</div>
                    </div>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.page-content -->
        <!-- Modal -->
        <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" >新增</h4>
                    </div>
                    <div class="modal-body" id="add-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right" for="add_relation_name" >关系名称</label>
                                <div class="col-sm-9">
                                    <input type="text" id="add_relation_name" placeholder="关系名称" class="col-xs-10 col-sm-5" />
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" id="add" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">修改</h4>
                    </div>
                    <div class="modal-body" id="update-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right" for="update_relation_name" >关系名称</label>
                                <div class="col-sm-9">
                                    <input type="hidden" id="id" value="0"/>
                                    <input type="text" id="update_relation_name" placeholder="关系名称" class="col-xs-10 col-sm-5" />
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" id="update" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div><!-- /.main-content -->
{/block}

{block name='footer'}
{include file='comm/footer'}
{/block}
{block name='script'}
<!--[if !IE]> -->
<script src="__ace_js__/jquery-2.1.4.min.js"></script>

<!-- <![endif]-->

<!--[if IE]>
<script src="__ace_js__/jquery-1.11.3.min.js"></script>
<![endif]-->
<script type="text/javascript">
    if('ontouchstart' in document.documentElement) document.write("<script src='__ace_js__/jquery.mobile.custom.min.js'>"+"<"+"/script>");
</script>
<script src="__ace_js__/bootstrap.min.js"></script>

<!-- page specific plugin scripts -->
<script src="__ace_js__/jquery.dataTables.min.js"></script>
<script src="__ace_js__/jquery.dataTables.bootstrap.min.js"></script>
<script src="__ace_js__/dataTables.buttons.min.js"></script>
<script src="__ace_js__/buttons.flash.min.js"></script>
<script src="__ace_js__/buttons.html5.min.js"></script>
<script src="__ace_js__/buttons.print.min.js"></script>
<script src="__ace_js__/buttons.colVis.min.js"></script>
<script src="__ace_js__/dataTables.select.min.js"></script>

<!-- ace scripts -->
<script src="__ace_js__/ace-elements.min.js"></script>
<script src="__ace_js__/ace.min.js"></script>

<!-- inline scripts related to this page -->
<script type="text/javascript">
    $('.green').on('click', function () {
        var id = $(this).data('id');
        $.ajax({
            url: "/admin/relation/"+id,
            type: 'get',
            dataType: 'json',
            success: function (json) {
                var data = json.data;
                if(!isEmpty(data)){
                    $('#id').val(id);
                    $('#update_relation_name').val(data.name);
                }
            }
        })
    })

    $('#add').on('click',function () {
        var relation_name = $('#add_relation_name').val();
        if(relation_name == ''){
            swal({
                type: 'warning', // 弹框类型
                title: '请输入关系名称', //标题
                confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                confirmButtonText: '确定',// 确定按钮的 文字
            });
            return false;
        }
        $.ajax({
            url:"{:url('relation/store')}",
            type:'post',
            dataType:'JSON',
            data:{'relation_name':relation_name},
            success:function(res){
                if(res.code == 1){
                    swal({
                        type: 'success', // 弹框类型
                        title: '操作成功', //标题
                        confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                        confirmButtonText: '确定',// 确定按钮的 文字
                    }).then((isConfirm) => {
                        //判断 是否 点击的 确定按钮
                        if (isConfirm.value) {
                            location.href = "{:url('admin.relation.index')}";
                        }
                    });
                }else{
                    swal({
                        type: 'warning', // 弹框类型
                        title:res.msg, //标题
                        confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                        confirmButtonText: '确定',// 确定按钮的 文字
                    });
                }
                return false;
            },error:function(e){
                swal({
                    type: 'error', // 弹框类型
                    title:e.msg, //标题
                    confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                    confirmButtonText: '确定',// 确定按钮的 文字
                });
            }
        })
    })

    $('#update').on('click',function () {
        var id = $('#id').val();
        var relation_name = $('#update_relation_name').val();
        if(relation_name == ''){
            swal({
                type: 'warning', // 弹框类型
                title: '请输入关系名称', //标题
                confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                confirmButtonText: '确定',// 确定按钮的 文字
            });
            return false;
        }
        $.ajax({
            url:"/admin/relation/"+id,
            type:'PUT',
            dataType:'JSON',
            data:{'relation_name':relation_name},
            success:function(res){
                if(res.code == 1){
                    swal({
                        type: 'success', // 弹框类型
                        title: '操作成功', //标题
                        confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                        confirmButtonText: '确定',// 确定按钮的 文字
                    }).then((isConfirm) => {
                        //判断 是否 点击的 确定按钮
                        if (isConfirm.value) {
                            location.href = "{:url('admin.relation.index')}";
                        }
                    });
                }else{
                    swal({
                        type: 'warning', // 弹框类型
                        title:res.msg, //标题
                        confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                        confirmButtonText: '确定',// 确定按钮的 文字
                    });
                }
                return false;
            },error:function(e){
                swal({
                    type: 'error', // 弹框类型
                    title:e.msg, //标题
                    confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                    confirmButtonText: '确定',// 确定按钮的 文字
                });
            }
        })
    })

    function isEmpty(obj){
        for(var key in obj){
            if(key){
                return false
            }
        }
        return true
    }
    function delete_relation(id){
        swal({
            type: 'warning', // 弹框类型
            title: '删除该记录吗', //标题
            text: "该操作将同时删除用户关系对应的设置数据，请谨慎操作！", //显示内容
            confirmButtonColor: '#3085d6',// 确定按钮的 颜色
            confirmButtonText: '确定',// 确定按钮的 文字
            showCancelButton: true, // 是否显示取消按钮
            cancelButtonColor: '#d33', // 取消按钮的 颜色
            cancelButtonText: "取消", // 取消按钮的 文字
            focusCancel: true, // 是否聚焦 取消按钮
            reverseButtons: true  // 是否 反转 两个按钮的位置 默认是  左边 确定  右边 取消
        }).then((isConfirm) => {
            try {
                //判断 是否 点击的 确定按钮
                if (isConfirm.value) {
                    $.ajax({
                        url:"/admin/relation/"+id,
                        type:'DELETE',
                        dataType:'JSON',
                        success:function(res){
                            if(res.code == 1){
                                swal({
                                    type: 'success', // 弹框类型
                                    title: '删除成功', //标题
                                    confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                                    confirmButtonText: '确定',// 确定按钮的 文字
                                }).then((confirm)=>{
                                    if(confirm){
                                       location.reload();
                                    }
                                });
                            }else{
                                swal({
                                    type: 'warning', // 弹框类型
                                    title:res.msg, //标题
                                    confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                                    confirmButtonText: '确定',// 确定按钮的 文字
                                });
                            }
                            return false;
                        },error:function($e){

                        }
                    })
                }
            } catch (e) {
                swal({
                    type: 'error', // 弹框类型
                    title:e, //标题
                    confirmButtonColor: '#438EB9',// 确定按钮的 颜色
                    confirmButtonText: '确定',// 确定按钮的 文字
                });
            }
        });

    }
    jQuery(function($) {
        //initiate dataTables plugin
        var myTable =
            $('#dynamic-table')
                .DataTable( {
                    bAutoWidth: false,
                    "aoColumns": [
                        { "bSortable": false },
                        null, null,null,
                        { "bSortable": false }
                    ],
                    "aaSorting": [],
                } );

        $.fn.dataTable.Buttons.defaults.dom.container.className = 'dt-buttons btn-overlap btn-group btn-overlap';

        //style the message box
        var defaultCopyAction = myTable.button(1).action();
        myTable.button(1).action(function (e, dt, button, config) {
            defaultCopyAction(e, dt, button, config);
            $('.dt-button-info').addClass('gritter-item-wrapper gritter-info gritter-center white');
        });

        var defaultColvisAction = myTable.button(0).action();
        myTable.button(0).action(function (e, dt, button, config) {

            defaultColvisAction(e, dt, button, config);


            if($('.dt-button-collection > .dropdown-menu').length == 0) {
                $('.dt-button-collection')
                    .wrapInner('<ul class="dropdown-menu dropdown-light dropdown-caret dropdown-caret" />')
                    .find('a').attr('href', '#').wrap("<li />")
            }
            $('.dt-button-collection').appendTo('.tableTools-container .dt-buttons')
        });

        ////

        setTimeout(function() {
            $($('.tableTools-container')).find('a.dt-button').each(function() {
                var div = $(this).find(' > div').first();
                if(div.length == 1) div.tooltip({container: 'body', title: div.parent().text()});
                else $(this).tooltip({container: 'body', title: $(this).text()});
            });
        }, 500);


        $(document).on('click', '#dynamic-table .dropdown-toggle', function(e) {
            e.stopImmediatePropagation();
            e.stopPropagation();
            e.preventDefault();
        });

    })
</script>
{/block}






